import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STChange, STPage, STComponent } from '@delon/abc';
import { SFSchema,SFTreeSelectWidgetSchema, SFSchemaEnum, SFComponent } from '@delon/form';
import { GridComponent, PopdetailComponent, HttpService, SupDic } from 'lbf';
import { NzMessageService } from 'ng-zorro-antd';
import { of,from } from 'rxjs';
import { CzyQxxqComponent } from '../qxxq/qxxq.component';

@Component({
  selector: 'app-czy-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class CzyListComponent implements OnInit {
  // datas = 'MohrssExInterface/gridsvr';
  params = { sname: "FR_quserlist", form: {} };

  isVisible = false;

  formData = {};

  newSchema: SFSchema = {
    properties: {
      czyxm: {
        type: 'string',
        title: '姓名',
      },
      uname: {
        type: 'string',
        title: '帐号名称',
      },
      phone: {
        type: 'string',
        format: 'mobile',
        title: '手机号码',
      },
    },
    ui: {
      spanLabelFixed: 100,
      grid: {
        span: 24
      }
    },
    required: ["czyxm", "uname", "phone"]
  }

  deptlist=[];
  rolelist=[];
  searchSchema: SFSchema = {
    properties: {

    },
  };

initSearchSchema(){
  this.searchSchema= {
    properties: {
      aac003: {
        type: 'string',
        title: '操作员姓名',
        ui:{
          width:250,
        }
      },
      zt: {
        type: 'string',
        title: '用户状态',
        enum:this.supdic.getSFDic('zbzt'),
        ui:{
          widget:'select',
          width:250,
          allowClear: true,
        }
      },
      dept: {
        type: 'string',
        title: '部门',
        enum: this.deptlist,
        ui:{
          widget:'tree-select',
          width:250,
          dropdownStyle:{
            height:'200px',
            width:'350px',
            overflow:'auto',
          },
          allowClear: true,
        }
      },
      role:{
        type: 'string',
        title: '角色',
        enum: this.rolelist,
        ui:{
          widget:'select',
          width:250,
          allowClear: true,
        }
      }
    },
  };
}

  //@ViewChild('st',{static:false}) st: GridComponent;
  @ViewChild('pop',{static:false}) pop: SFComponent;
  columns: STColumn[] = [
    { title: '用户状态', index: 'STATUS',render:'custom' },
    { title: '用户ID', index: 'USERID' },
    { title: '操作员姓名', index: 'CZYXM', statistical: 'count' },
    { title: '开始时间', index: 'KSSJ' },
    { title: '权限',  buttons: [
      {
        text: '权限详情',
        type:'modal',
        modal:{
          component:CzyQxxqComponent,
        },
        click: (_record) =>{},
      }
    ],
  },
    {
      title: '操作区',
      buttons: [
        {
          text: '重置密码',
          click: (record: any) => this.pwdChang(record),
          pop: true,
          popTitle: '确认重置该账号密码？',
        },
         {
          text: '修改',
          click: (record: any) => this.update(record),
          pop: true,
          popTitle: '确认修改该账号？',
        },
      ],
    },
  ];

  //修改
  @ViewChild('czyupPop',{static:false}) czyupPop: SFComponent;
  czyupSchema:SFSchema={
    properties:{

    }
  }
  czyupVisible=false;
  update(rec:any){
    this.czyupSchema={
      properties: {
        USERID: {
          type: 'string',
          title: '操作员',
          default:rec.QXXY,
          ui:{
            hidden:true,
          }
        },
        UNAME: {
          type: 'string',
          title: '登录名',
          default:rec.USERID,
        },
        CZYXM:{
          type: 'string',
          title: '登录名',
          default:rec.CZYXM,
        },
      },
      ui: {
        spanLabelFixed: 100,
        grid: {
          span: 12
        }
      },
    };
    this.czyupVisible=true;
  }

  czyuphandleCancel() {
    this.czyupVisible = false;
  }
  
  czyuphandleOk(){
    this.lbservice.lbservice("czygl_czyupdate",{para:this.czyupPop.value}).then(resdata=>{
      if(resdata.code>0){
        this.msgSrv.success("更改成功");
      }else{
        this.msgSrv.error(resdata.errmsg);
      }
      this.czyupVisible=false;
      this.queryDataset();
    });
  }



  isConfirmLoading = false;

  constructor(
    private modal: ModalHelper,
    public msgSrv: NzMessageService,
    private lbservice: HttpService,
    private supdic: SupDic,
  ) { }

  ngOnInit() {
    this.initDataset();
    //初始化部门列表
    this.lbservice.lbservice("qxgl_deptlist",{}).then(resdata=>{
      this.deptlist=resdata.message.list;
    //初始化角色列表
    this.lbservice.lbservice("czygl_qrolelist",{}).then(resdatae=>{
      this.rolelist=resdatae.message.list;
      this.rolelist.forEach(ele=>{
        ele.label=ele.KEY;
        ele.value=ele.VALUE;
      });
      this.initSearchSchema();
    });
    });
  }

  add() {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());

    this.isVisible = true;
  }

  save(val: any, resdata: any) {

    //  if(resdata.code>0){
    //    this.st.reload();
    //  }
  }


  delczy(rec: any) {
    if (rec.USERTYPE === '0') {
      this.msgSrv.error('无法删除设计者帐号，请联系信息中心！');
      return;
    }

    this.st.loading = true;

    this.lbservice
      .lbservice('FR_quserdel', { para: { USERID: rec.USERID } })
      .then(resdata => {
        this.isConfirmLoading = false;
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {

          this.msgSrv.info('注销成功！');
          this.st.reload();
        }

        this.st.loading = false;
      });
  }

  pwdChang(rec: any) {
    this.st.loading = true;

    this.lbservice
      .lbservice('FR_qpwdChang', { para: { USERID: rec.USERID } })
      .then(resdata => {
        this.isConfirmLoading = false;
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {

          this.msgSrv.info('密码重置成功！');
          this.st.reload();
        }

        this.st.loading = false;
      });

  }


  export() {

    this.st.export(this.st._data,{ filename: 'test.xlsx', sheetname: 'sheet1' });

  }

  query(){
    this.stchange.pi=1;
    this.queryDataset();
  }


  /************************************** */
  @ViewChild('sf',{static:false}) sf: SFComponent;
  @ViewChild('st',{static:false}) st: STComponent;
    //st分页相关
    stpage: STPage = {
      total: true,
      show: true,//显示分页
      front: false //关闭前端分页，true是前端分页，false后端控制分页
    };
    stchange:STChange={
      type:"pi",
      pi:1,
      ps:10,
      total:0,
    }
    dataset=[];

    initDataset(){
      this.lbservice.lbservice("czy_quserlist",{page:{'PAGESIZE':this.stchange.ps,'PAGEIDX':this.stchange.pi}}).then(resdata=>{
        this.dataset=resdata.message.list;
        this.stchange.total=resdata.page.COUNT;
      });
    }

    queryDataset(){
      this.lbservice.lbservice("czy_quserlist",{para:this.sf.value,page:{'PAGESIZE':this.stchange.ps,'PAGEIDX':this.stchange.pi}}).then(resdata=>{
        this.dataset=resdata.message.list;
        this.stchange.total=resdata.page.COUNT;
      });
    }

    pageChange(para:any){
      if(para.type=="pi"){
        console.log(this.sf.value);
       this.lbservice.lbservice('czy_quserlist',{para:this.sf.value,page:{'PAGESIZE':this.stchange.ps,'PAGEIDX':para.pi}}).then(resdata=>{
         if(resdata.code==1){
           this.dataset=resdata.message.list;
           this.stchange.pi=para.pi;
         }
      });
    }
   }

   changeState(index:any){
    let rec:any=this.st._data[index];
    if(rec.STATUS=='0'){
      rec.STATUS='1';
    }else if(rec.STATUS=='1'){
      rec.STATUS='0';
    }
    this.lbservice.lbservice('czy_upstatus',{'USERID':rec.QXXY,'STATUS':rec.STATUS}).then(resdata=>{
      if(resdata.code==1){
        this.st._data[index].AAE100=rec.AAE100;
      }
    });
  }
    

  handleCancel() {
    this.isVisible = false;
  }

  handleOk(){
    this.lbservice.lbservice("FR_quseradd",{para:this.pop.value}).then(resdata=>{
      if(resdata.code>0){
        this.msgSrv.success("新建成功");
      }else{
        this.msgSrv.error(resdata.errmsg);
      }
      this.isVisible=false;
      this.st.reload();
    });
  }

}
